package com.cyys.modules.core.sysParam;

import com.cyys.common.redis.RedisUtil;
import com.cyys.modules.system.model.configure.Configure;
import com.cyys.modules.system.service.configure.ConfigureService;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class SystemConfigureUtil {

	public static void init(ConfigureService configureService) {
		if(RedisUtil.hSize(RedisConstants.SYSTEM_CONFIGURE) >= 0){
			RedisUtil.delete(RedisConstants.SYSTEM_CONFIGURE);
		}
		List<Configure> list = configureService.listAll(null);
		for (Configure entity : list) {
			RedisUtil.hPut(RedisConstants.SYSTEM_CONFIGURE,entity.getName(),entity.getValue());
		}
	}

	public static Map<Object, Object> getAll() {
		return RedisUtil.hGetAll(RedisConstants.SYSTEM_CONFIGURE);
	}

	public static String get(String name){
		return (String)RedisUtil.hGet(RedisConstants.SYSTEM_CONFIGURE, name);
	}

	public static void add(Configure entity) {
		RedisUtil.hPut(RedisConstants.SYSTEM_CONFIGURE,entity.getName(),entity.getValue());
	}

	public static void delete(String name) {
		RedisUtil.hDelete(RedisConstants.SYSTEM_CONFIGURE,name);
	}

	public static void modify(Configure entity) {
		RedisUtil.hDelete(RedisConstants.SYSTEM_CONFIGURE,entity.getName());
		RedisUtil.hPut(RedisConstants.SYSTEM_CONFIGURE,entity.getName(),entity.getValue());
	}
}
